package it.polimi.TravelDream;

import java.io.Serializable;
import java.math.BigInteger;

import javax.persistence.*;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * The persistent class for the user database table.
 * 
 */
@Entity
@NamedQueries({
@NamedQuery(name="User.findAll", query="SELECT u FROM User u"),
@NamedQuery(name="GetCurrent", query="SELECT u FROM User u WHERE u.email LIKE :param" )})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final String GetCurrent = "GetCurrent";

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;

	@Temporal(TemporalType.DATE)
	private Date BirthDate;

	private String email;

	private String name;

	private String password;

	private String surname;

	private BigInteger telephone;

	//bi-directional many-to-one association to Flight
	@OneToMany(mappedBy="user")
	private List<Flight> flights;

	//bi-directional many-to-one association to Hotel
	@OneToMany(mappedBy="user")
	private List<Hotel> hotels;

	//bi-directional many-to-one association to Savedpackage
	@OneToMany(mappedBy="user")
	private List<Savedpackage> savedpackages;

	//bi-directional many-to-one association to Basepackage
	@OneToMany(mappedBy="user")
	private List<Basepackage> basepackages;

	//bi-directional many-to-one association to Excursion
	@OneToMany(mappedBy="user")
	private List<Excursion> excursions;

	//bi-directional many-to-one association to Packagejoin
	@OneToMany(mappedBy="user")
	private List<Packagejoin> packagejoins;
	
	@ElementCollection(targetClass = Groups_Names.class)
    @CollectionTable(name = "user_group",
                    joinColumns = @JoinColumn (name = "email",referencedColumnName="email",unique =true, nullable=false)
                    )
    @Enumerated(EnumType.STRING)
	@Column(name="groupname")
	
    private List<Groups_Names> groups;

	public User() {
		super();
	}
	
	public User(UserDTO user){
		
        this.name   = user.getName();
        this.surname     = user.getSurname();        
        this.email        = user.getEmail();
        this.password     = DigestUtils.sha256Hex(user.getPassword() );
        this.BirthDate = user.getBirthday();
        this.telephone = user.getTelephone();
        this.groups = new ArrayList<Groups_Names>();
	}
	
	public User(UserDTO user, int ID){
		this.name   = user.getName();
        this.surname     = user.getSurname();        
        this.email        = user.getEmail();
        this.password     = user.getPassword();
        this.BirthDate = user.getBirthday();
        this.telephone = user.getTelephone();
        this.id = user.getID();
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getBirthDate() {
		return this.BirthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.BirthDate = birthDate;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSurname() {
		return this.surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public BigInteger getTelephone() {
		return this.telephone;
	}

	public void setTelephone(BigInteger telephone) {
		this.telephone = telephone;
	}
	
	public void setGroups(Groups_Names group) {
        this.groups.add(group);
        
    }

	public List<Flight> getFlights() {
		return this.flights;
	}

	public void setFlights(List<Flight> flights) {
		this.flights = flights;
	}

	public Flight addFlight(Flight flight) {
		getFlights().add(flight);
		flight.setUser(this);

		return flight;
	}

	public Flight removeFlight(Flight flight) {
		getFlights().remove(flight);
		flight.setUser(null);

		return flight;
	}

	public List<Hotel> getHotels() {
		return this.hotels;
	}

	public void setHotels(List<Hotel> hotels) {
		this.hotels = hotels;
	}

	public Hotel addHotel(Hotel hotel) {
		getHotels().add(hotel);
		hotel.setUser(this);

		return hotel;
	}

	public Hotel removeHotel(Hotel hotel) {
		getHotels().remove(hotel);
		hotel.setUser(null);

		return hotel;
	}

	public List<Savedpackage> getSavedpackages() {
		return this.savedpackages;
	}

	public void setSavedpackages(List<Savedpackage> savedpackages) {
		this.savedpackages = savedpackages;
	}

	public Savedpackage addSavedpackage(Savedpackage savedpackage) {
		getSavedpackages().add(savedpackage);
		savedpackage.setUser(this);

		return savedpackage;
	}

	public Savedpackage removeSavedpackage(Savedpackage savedpackage) {
		getSavedpackages().remove(savedpackage);
		savedpackage.setUser(null);

		return savedpackage;
	}

	public List<Basepackage> getBasepackages() {
		return this.basepackages;
	}

	public void setBasepackages(List<Basepackage> basepackages) {
		this.basepackages = basepackages;
	}

	public Basepackage addBasepackage(Basepackage basepackage) {
		getBasepackages().add(basepackage);
		basepackage.setUser(this);

		return basepackage;
	}

	public Basepackage removeBasepackage(Basepackage basepackage) {
		getBasepackages().remove(basepackage);
		basepackage.setUser(null);

		return basepackage;
	}

	public List<Excursion> getExcursions() {
		return this.excursions;
	}

	public void setExcursions(List<Excursion> excursions) {
		this.excursions = excursions;
	}

	public Excursion addExcursion(Excursion excursion) {
		getExcursions().add(excursion);
		excursion.setUser(this);

		return excursion;
	}

	public Excursion removeExcursion(Excursion excursion) {
		getExcursions().remove(excursion);
		excursion.setUser(null);

		return excursion;
	}

	public List<Packagejoin> getPackagejoins() {
		return this.packagejoins;
	}

	public void setPackagejoins(List<Packagejoin> packagejoins) {
		this.packagejoins = packagejoins;
	}

	public Packagejoin addPackagejoin(Packagejoin packagejoin) {
		getPackagejoins().add(packagejoin);
		packagejoin.setUser(this);

		return packagejoin;
	}

	public Packagejoin removePackagejoin(Packagejoin packagejoin) {
		getPackagejoins().remove(packagejoin);
		packagejoin.setUser(null);

		return packagejoin;
	}

}